package org.springframework.beans.factory.support;

import org.apache.commons.logging.Log;
import org.springframework.beans.*;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.core.CollectionFactory;
import org.springframework.core.MethodParameter;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.lang.Nullable;
import org.springframework.util.*;

import java.beans.ConstructorProperties;
import java.lang.reflect.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;

/**
 * Delegate for resolving constructors and factory methods.
 * <p>Performs constructor resolution through argument matching.
 * @author Juergen Hoeller
 * @author Rob Harrop
 * @author Mark Fisher
 * @author Costin Leau
 * @author Sebastien Deleuze
 * @author Sam Brannen
 * @see #autowireConstructor
 * @see #instantiateUsingFactoryMethod
 * @see AbstractAutowireCapableBeanFactory
 * @since 2.0
 */
class ConstructorResolver {

    private static final Object[] EMPTY_ARGS = new Object[0];

    /**
     * Marker for autowired arguments in a cached argument array, to be later replaced
     * by a {@linkplain #resolveAutowiredArgument resolved autowired argument}.
     */
    private static final Object autowiredArgumentMarker = new Object();

    private static final NamedThreadLocal<InjectionPoint> currentInjectionPoint = new NamedThreadLocal<>("Current "
            + "injection point");

    private final AbstractAutowireCapableBeanFactory beanFactory;

    private final Log logger;

    /**
     * Create a new ConstructorResolver for the given factory and instantiation strategy.
     * @param beanFactory the BeanFactory to work with
     */
    public ConstructorResolver(AbstractAutowireCapableBeanFactory beanFactory) {
        this.beanFactory = beanFactory;
        this.logger = beanFactory.getLogger();
    }

    /**
     * "autowire constructor" (with constructor arguments by type) behavior.
     * Also applied if explicit constructor argument values are specified,
     * matching all remaining arguments with beans from the bean factory.
     * <p>This corresponds to constructor injection: In this mode, a Spring
     * bean factory is able to host components that expect constructor-based
     * dependency resolution.
     * @param beanName     the name of the bean
     * @param mbd          the merged bean definition for the bean
     * @param chosenCtors  chosen candidate constructors (or {@code null} if none)
     * @param explicitArgs argument values passed in programmatically via the getBean method,
     *                     or {@code null} if none (-> use constructor argument values from bean definition)
     * @return a BeanWrapper for the new instance
     */
    public BeanWrapper autowireConstructor(String beanName, RootBeanDefinition mbd,
                                           @Nullable Constructor<?>[] chosenCtors, @Nullable Object[] explicitArgs) {

        BeanWrapperImpl bw = new BeanWrapperImpl();
        this.beanFactory.initBeanWrapper(bw);

        Constructor<?> constructorToUse = null;
        ArgumentsHolder argsHolderToUse = null;
        Object[] argsToUse = null;
        // explicit通过getBean方法传入
        // 如果getBean方法调用的时候指定方法参数，那么直接使用
        if(explicitArgs != null){
            argsToUse = explicitArgs;
        }else{
            // 如果在getBean方法时候没有指定则尝试从配置文件中解析
            Object[] argsToResolve = null;
            // 尝试从缓存中获取获取参数
            synchronized(mbd.constructorArgumentLock){
                constructorToUse = (Constructor<?>)mbd.resolvedConstructorOrFactoryMethod;
                if(constructorToUse != null && mbd.constructorArgumentsResolved){
                    // Found a cached constructor...
                    // 缓存中的构造参数
                    argsToUse = mbd.resolvedConstructorArguments;
                    if(argsToUse == null){
                        argsToResolve = mbd.preparedConstructorArguments;
                    }
                }
            }
            // 如果缓存中存在
            if(argsToResolve != null){
                // 解析参数类型，如给定方法的构造函数A(int,int)则通过此方法后就会
                // 把配置中("1","1")转换为(1,1)
                // 缓存中的值可能是原始值也可能是最终值，在这里做处理
                argsToUse = resolvePreparedArguments(beanName, mbd, bw, constructorToUse, argsToResolve, true);
            }
        }
        // 没有缓存也没有明确指定
        if(constructorToUse == null || argsToUse == null){
            // Take specified constructors, if any.
            Constructor<?>[] candidates = chosenCtors;
            // 如果参数中没有指定构造函数
            if(candidates == null){
                // 获取当前bean对应的class对象
                Class<?> beanClass = mbd.getBeanClass();
                try{
                    // 获取所有的构造函数
                    candidates = (mbd.isNonPublicAccessAllowed()?beanClass.getDeclaredConstructors():
                                  beanClass.getConstructors());
                }catch(Throwable ex){
                    throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                            "Resolution of declared constructors on bean Class ["
                                    + beanClass.getName()
                                    + "] from ClassLoader ["
                                    + beanClass.getClassLoader()
                                    + "] failed", ex);
                }
            }
            // 如果只有默认构造函数并且配置文件中也没有参数配置
            if(candidates.length == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()){
                Constructor<?> uniqueCandidate = candidates[0];
                if(uniqueCandidate.getParameterCount() == 0){
                    // 缓存构造函数
                    synchronized(mbd.constructorArgumentLock){
                        mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
                        mbd.constructorArgumentsResolved = true;
                        mbd.resolvedConstructorArguments = EMPTY_ARGS;
                    }
                    // 使用默认构造函数初始化对象
                    bw.setBeanInstance(instantiate(beanName, mbd, uniqueCandidate, EMPTY_ARGS));
                    return bw;
                }
            }

            // Need to resolve the constructor.
            boolean autowiring = (chosenCtors != null
                    || mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
            ConstructorArgumentValues resolvedValues = null;

            int minNrOfArgs;
            if(explicitArgs != null){
                minNrOfArgs = explicitArgs.length;
            }else{
                ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
                resolvedValues = new ConstructorArgumentValues();
                minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
            }

            AutowireUtils.sortConstructors(candidates);
            int minTypeDiffWeight = Integer.MAX_VALUE;
            Set<Constructor<?>> ambiguousConstructors = null;
            LinkedList<UnsatisfiedDependencyException> causes = null;

            for(Constructor<?> candidate: candidates){
                Class<?>[] paramTypes = candidate.getParameterTypes();

                if(constructorToUse != null && argsToUse != null && argsToUse.length > paramTypes.length){
                    // Already found greedy constructor that can be satisfied ->
                    // do not look any further, there are only less greedy constructors left.
                    break;
                }
                if(paramTypes.length < minNrOfArgs){
                    continue;
                }

                ArgumentsHolder argsHolder;
                if(resolvedValues != null){
                    try{
                        String[] paramNames = ConstructorPropertiesChecker.evaluate(candidate, paramTypes.length);
                        if(paramNames == null){
                            ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
                            if(pnd != null){
                                paramNames = pnd.getParameterNames(candidate);
                            }
                        }
                        argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes, paramNames,
                                getUserDeclaredConstructor(candidate), autowiring,
                                candidates.length
                                        == 1);
                    }catch(UnsatisfiedDependencyException ex){
                        if(logger.isTraceEnabled()){
                            logger.trace("Ignoring constructor [" + candidate + "] of bean '" + beanName + "': " + ex);
                        }
                        // Swallow and try next constructor.
                        if(causes == null){
                            causes = new LinkedList<>();
                        }
                        causes.add(ex);
                        continue;
                    }
                }else{
                    // Explicit arguments given -> arguments length must match exactly.
                    if(paramTypes.length != explicitArgs.length){
                        continue;
                    }
                    argsHolder = new ArgumentsHolder(explicitArgs);
                }

                int typeDiffWeight = (mbd.isLenientConstructorResolution()?
                                      argsHolder.getTypeDifferenceWeight(paramTypes):argsHolder
                        .getAssignabilityWeight(paramTypes));
                // Choose this constructor if it represents the closest match.
                if(typeDiffWeight < minTypeDiffWeight){
                    constructorToUse = candidate;
                    argsHolderToUse = argsHolder;
                    argsToUse = argsHolder.arguments;
                    minTypeDiffWeight = typeDiffWeight;
                    ambiguousConstructors = null;
                }else if(constructorToUse != null && typeDiffWeight == minTypeDiffWeight){
                    if(ambiguousConstructors == null){
                        ambiguousConstructors = new LinkedHashSet<>();
                        ambiguousConstructors.add(constructorToUse);
                    }
                    ambiguousConstructors.add(candidate);
                }
            }

            if(constructorToUse == null){
                if(causes != null){
                    UnsatisfiedDependencyException ex = causes.removeLast();
                    for(Exception cause: causes){
                        this.beanFactory.onSuppressedException(cause);
                    }
                    throw ex;
                }
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Could not resolve matching constructor "
                                + "(hint: specify index/type/name arguments for simple parameters to avoid type "
                                + "ambiguities)");
            }else if(ambiguousConstructors != null && !mbd.isLenientConstructorResolution()){
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Ambiguous constructor matches found in bean '"
                                + beanName
                                + "' "
                                + "(hint: specify index/type/name arguments for simple parameters to avoid type "
                                + "ambiguities): "
                                + ambiguousConstructors);
            }

            if(explicitArgs == null && argsHolderToUse != null){
                argsHolderToUse.storeCache(mbd, constructorToUse);
            }
        }

        Assert.state(argsToUse != null, "Unresolved constructor arguments");
        bw.setBeanInstance(instantiate(beanName, mbd, constructorToUse, argsToUse));
        return bw;
    }

    private Object instantiate(String beanName, RootBeanDefinition mbd, Constructor<?> constructorToUse,
                               Object[] argsToUse) {

        try{
            InstantiationStrategy strategy = this.beanFactory.getInstantiationStrategy();
            if(System.getSecurityManager() != null){
                return AccessController.doPrivileged((PrivilegedAction<Object>)() -> strategy.instantiate(mbd,
                        beanName, this.beanFactory, constructorToUse, argsToUse), this.beanFactory
                        .getAccessControlContext());
            }else{
                return strategy.instantiate(mbd, beanName, this.beanFactory, constructorToUse, argsToUse);
            }
        }catch(Throwable ex){
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean instantiation via "
                    + "constructor failed", ex);
        }
    }

    /**
     * Resolve the factory method in the specified bean definition, if possible.
     * {@link RootBeanDefinition#getResolvedFactoryMethod()} can be checked for the result.
     * @param mbd the bean definition to check
     */
    public void resolveFactoryMethodIfPossible(RootBeanDefinition mbd) {
        Class<?> factoryClass;
        boolean isStatic;
        if(mbd.getFactoryBeanName() != null){
            factoryClass = this.beanFactory.getType(mbd.getFactoryBeanName());
            isStatic = false;
        }else{
            factoryClass = mbd.getBeanClass();
            isStatic = true;
        }
        Assert.state(factoryClass != null, "Unresolvable factory class");
        factoryClass = ClassUtils.getUserClass(factoryClass);

        Method[] candidates = getCandidateMethods(factoryClass, mbd);
        Method uniqueCandidate = null;
        for(Method candidate: candidates){
            if(Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)){
                if(uniqueCandidate == null){
                    uniqueCandidate = candidate;
                }else if(!Arrays.equals(uniqueCandidate.getParameterTypes(), candidate.getParameterTypes())){
                    uniqueCandidate = null;
                    break;
                }
            }
        }
        mbd.factoryMethodToIntrospect = uniqueCandidate;
    }

    /**
     * Retrieve all candidate methods for the given class, considering
     * the {@link RootBeanDefinition#isNonPublicAccessAllowed()} flag.
     * Called as the starting point for factory method determination.
     */
    private Method[] getCandidateMethods(Class<?> factoryClass, RootBeanDefinition mbd) {
        if(System.getSecurityManager() != null){
            return AccessController.doPrivileged((PrivilegedAction<Method[]>)() -> (mbd.isNonPublicAccessAllowed()?
                                                                                    ReflectionUtils
                    .getAllDeclaredMethods(factoryClass):factoryClass.getMethods()));
        }else{
            return (mbd.isNonPublicAccessAllowed()?ReflectionUtils.getAllDeclaredMethods(factoryClass):
                    factoryClass.getMethods());
        }
    }

    /**
     * Instantiate the bean using a named factory method. The method may be static, if the
     * bean definition parameter specifies a class, rather than a "factory-bean", or
     * an instance variable on a factory object itself configured using Dependency Injection.
     * <p>Implementation requires iterating over the static or instance methods with the
     * name specified in the RootBeanDefinition (the method may be overloaded) and trying
     * to match with the parameters. We don't have the types attached to constructor args,
     * so trial and error is the only way to go here. The explicitArgs array may contain
     * argument values passed in programmatically via the corresponding getBean method.
     * @param beanName     the name of the bean
     * @param mbd          the merged bean definition for the bean
     * @param explicitArgs argument values passed in programmatically via the getBean
     *                     method, or {@code null} if none (-> use constructor argument values from bean definition)
     * @return a BeanWrapper for the new instance
     */
    public BeanWrapper instantiateUsingFactoryMethod(String beanName, RootBeanDefinition mbd,
                                                     @Nullable Object[] explicitArgs) {

        BeanWrapperImpl bw = new BeanWrapperImpl();
        // 初始化 BeanWrapperImpl
        // 向BeanWrapper对象中添加 ConversionService 对象和属性编辑器 PropertyEditor 对象
        this.beanFactory.initBeanWrapper(bw);

        Object factoryBean;
        Class<?> factoryClass;
        boolean isStatic;

        // 工厂名不为空
        String factoryBeanName = mbd.getFactoryBeanName();
        if(factoryBeanName != null){
            if(factoryBeanName.equals(beanName)){
                throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "factory-bean "
                        + "reference points back to the same bean definition");
            }
            // 获取工厂实例
            factoryBean = this.beanFactory.getBean(factoryBeanName);
            if(mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)){
                throw new ImplicitlyAppearedSingletonException();
            }
            factoryClass = factoryBean.getClass();
            isStatic = false;
        }else{
            // It's a static factory method on the bean class.
            // 工厂名为null，则其可能是一个静态工厂
            // 静态工厂创建bean，必须要提供工厂的全类名
            if(!mbd.hasBeanClass()){
                throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "bean definition "
                        + "declares neither a bean class nor a factory-bean reference");
            }
            factoryBean = null;
            factoryClass = mbd.getBeanClass();
            isStatic = true;
        }
        // 工厂方法
        Method factoryMethodToUse = null;
        ArgumentsHolder argsHolderToUse = null;
        Object[] argsToUse = null;
        // 工厂方法的参数
        // 如果指定了构造参数则直接使用
        // 在调用 getBean 方法的时候指定了方法参数
        if(explicitArgs != null){
            argsToUse = explicitArgs;
        }else{
            // 没有指定，则尝试从配置文件中解析
            Object[] argsToResolve = null;

            // 首先尝试从缓存中获取
            synchronized(mbd.constructorArgumentLock){
                factoryMethodToUse = (Method)mbd.resolvedConstructorOrFactoryMethod;

                // 获取缓存中的构造函数或者工厂方法
                if(factoryMethodToUse != null && mbd.constructorArgumentsResolved){
                    // Found a cached factory method...
                    // 获取缓存中的构造参数
                    argsToUse = mbd.resolvedConstructorArguments;
                    if(argsToUse == null){
                        // 获取缓存中的构造函数参数的包可见字段
                        argsToResolve = mbd.preparedConstructorArguments;
                    }
                }
            }
            // 缓存中存在,则解析存储在 BeanDefinition 中的参数
            // 如给定方法的构造函数 A(int ,int )，则通过此方法后就会把配置文件中的("1","1")转换为 (1,1)
            // 缓存中的值可能是原始值也有可能是最终值
            if(argsToResolve != null){
                argsToUse = resolvePreparedArguments(beanName, mbd, bw, factoryMethodToUse, argsToResolve, true);
            }
        }
        //  调用getBean方法没有传参，同时也是第一次使用工厂方法
        if(factoryMethodToUse == null || argsToUse == null){
            // Need to determine the factory method...
            // Try all methods with this name to see if they match the given arguments.
            // 获取工厂方法的类全名称
            factoryClass = ClassUtils.getUserClass(factoryClass);

            List<Method> candidateList = null;
            if(mbd.isFactoryMethodUnique){
                if(factoryMethodToUse == null){
                    factoryMethodToUse = mbd.getResolvedFactoryMethod();
                }
                // 获取所有符合条件的方法
                if(factoryMethodToUse != null){
                    candidateList = Collections.singletonList(factoryMethodToUse);
                }
            }
            if(candidateList == null){
                candidateList = new ArrayList<>();
                // 检索所有方法，这里是对方法进行过滤
                Method[] rawCandidates = getCandidateMethods(factoryClass, mbd);
                for(Method candidate: rawCandidates){
                    // 如果有static 且为工厂方法，则添加到 candidateSet 中
                    if(Modifier.isStatic(candidate.getModifiers()) == isStatic && mbd.isFactoryMethod(candidate)){
                        candidateList.add(candidate);
                    }
                }
            }
            //当与工厂方法同名的候选方法只有一个，且调用getBean方法时没有传参，
            //  且没有缓存过参数，直接通过调用实例化方法执行该候选方法
            if(candidateList.size() == 1 && explicitArgs == null && !mbd.hasConstructorArgumentValues()){
                Method uniqueCandidate = candidateList.get(0);
                if(uniqueCandidate.getParameterCount() == 0){
                    mbd.factoryMethodToIntrospect = uniqueCandidate;
                    synchronized(mbd.constructorArgumentLock){
                        mbd.resolvedConstructorOrFactoryMethod = uniqueCandidate;
                        mbd.constructorArgumentsResolved = true;
                        mbd.resolvedConstructorArguments = EMPTY_ARGS;
                    }
                    bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, uniqueCandidate, EMPTY_ARGS));
                    return bw;
                }
            }

            Method[] candidates = candidateList.toArray(new Method[0]);
            // 排序构造函数
            // public 构造函数优先参数数量降序，非public 构造函数参数数量降序
            AutowireUtils.sortFactoryMethods(candidates);
            // 有多个与工厂方法同名的候选方法时，进行排序。public的方法会往前排，然后参数个数多的方法往前排
            ConstructorArgumentValues resolvedValues = null;
            boolean autowiring = (mbd.getResolvedAutowireMode() == AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR);
            int minTypeDiffWeight = Integer.MAX_VALUE;
            Set<Method> ambiguousFactoryMethods = null;

            int minNrOfArgs;
            // 如果调用getBean方法时有传参，那么工厂方法最少参数个数要等于传参个数
            if(explicitArgs != null){
                minNrOfArgs = explicitArgs.length;
            }else{
                // We don't have arguments passed in programmatically, so we need to resolve the
                // arguments specified in the constructor arguments held in the bean definition.
                // getBean() 没有传递参数，则需要解析保存在 BeanDefinition 构造函数中指定的参数
                if(mbd.hasConstructorArgumentValues()){
                    // 构造函数的参数
                    ConstructorArgumentValues cargs = mbd.getConstructorArgumentValues();
                    resolvedValues = new ConstructorArgumentValues();
                    // 解析构造函数的参数
                    // 将该 bean 的构造函数参数解析为 resolvedValues 对象，其中会涉及到其他 bean
                    minNrOfArgs = resolveConstructorArguments(beanName, mbd, bw, cargs, resolvedValues);
                }else{
                    minNrOfArgs = 0;
                }
            }

            LinkedList<UnsatisfiedDependencyException> causes = null;
            // 遍历同名候选方法
            for(Method candidate: candidates){
                //   获取候选方法的参数列表
                Class<?>[] paramTypes = candidate.getParameterTypes();

                if(paramTypes.length >= minNrOfArgs){
                    ArgumentsHolder argsHolder;
                    //在调用getBean方法时传的参数不为空，则工厂方法的参数个数需要与
                    // 传入的参数个数严格一致
                    if(explicitArgs != null){
                        // Explicit arguments given -> arguments length must match exactly.
                        // 显示给定参数，参数长度必须完全匹配
                        if(paramTypes.length != explicitArgs.length){
                            continue;
                        }
                        argsHolder = new ArgumentsHolder(explicitArgs);
                    }else{
                        // Resolved constructor arguments: type conversion and/or autowiring necessary.
                        try{
                            String[] paramNames = null;
                            // 获取 ParameterNameDiscoverer 对象
                            // ParameterNameDiscoverer 是用于解析方法和构造函数的参数名称的接口，为参数名称探测器
                            ParameterNameDiscoverer pnd = this.beanFactory.getParameterNameDiscoverer();
                            if(pnd != null){
                                paramNames = pnd.getParameterNames(candidate);
                            }
                            //当传入的参数为空，需要根据工厂方法的参数类型注入相应的bean
                            argsHolder = createArgumentArray(beanName, mbd, resolvedValues, bw, paramTypes,
                                    paramNames, candidate, autowiring,
                                    candidates.length
                                            == 1);
                        }catch(UnsatisfiedDependencyException ex){
                            if(logger.isTraceEnabled()){
                                logger.trace("Ignoring factory method ["
                                        + candidate
                                        + "] of bean '"
                                        + beanName
                                        + "': "
                                        + ex);
                            }
                            // Swallow and try next overloaded factory method.
                            if(causes == null){
                                causes = new LinkedList<>();
                            }
                            causes.add(ex);
                            continue;
                        }
                    }
                    // isLenientConstructorResolution 判断解析构造函数的时候是否以宽松模式还是严格模式
                    // 严格模式：解析构造函数时，必须所有的都需要匹配，否则抛出异常
                    // 宽松模式：使用具有"最接近的模式"进行匹配
                    int typeDiffWeight = (mbd.isLenientConstructorResolution()?
                                          argsHolder.getTypeDifferenceWeight(paramTypes):argsHolder
                            .getAssignabilityWeight(paramTypes));
                    // Choose this factory method if it represents the closest match.
                    // 代表最接近的类型匹配，则选择作为构造函数
                    if(typeDiffWeight < minTypeDiffWeight){
                        factoryMethodToUse = candidate;
                        argsHolderToUse = argsHolder;
                        argsToUse = argsHolder.arguments;
                        minTypeDiffWeight = typeDiffWeight;
                        ambiguousFactoryMethods = null;
                    }
                    // Find out about ambiguity: In case of the same type difference weight
                    // for methods with the same number of parameters, collect such candidates
                    // and eventually raise an ambiguity exception.
                    // However, only perform that check in non-lenient constructor resolution mode,
                    // and explicitly ignore overridden methods (with the same parameter signature).
                    // 如果具有相同参数数量的方法具有相同的类型差异权重，则收集此类型选项
                    // 但是，仅在非宽松构造函数解析模式下执行该检查，并显式忽略重写方法（具有相同的参数签名）
                    else if(factoryMethodToUse != null
                            && typeDiffWeight == minTypeDiffWeight
                            && !mbd.isLenientConstructorResolution()
                            && paramTypes.length == factoryMethodToUse.getParameterCount()
                            && !Arrays.equals(paramTypes, factoryMethodToUse.getParameterTypes())){
                        // 查找到多个可匹配的方法
                        if(ambiguousFactoryMethods == null){
                            ambiguousFactoryMethods = new LinkedHashSet<>();
                            ambiguousFactoryMethods.add(factoryMethodToUse);
                        }
                        ambiguousFactoryMethods.add(candidate);
                    }
                }
            }
            // 没有可执行的工厂方法，抛出异常
            if(factoryMethodToUse == null || argsToUse == null){
                if(causes != null){
                    UnsatisfiedDependencyException ex = causes.removeLast();
                    for(Exception cause: causes){
                        this.beanFactory.onSuppressedException(cause);
                    }
                    throw ex;
                }
                List<String> argTypes = new ArrayList<>(minNrOfArgs);
                if(explicitArgs != null){
                    for(Object arg: explicitArgs){
                        argTypes.add(arg != null?arg.getClass().getSimpleName():"null");
                    }
                }else if(resolvedValues != null){
                    Set<ValueHolder> valueHolders = new LinkedHashSet<>(resolvedValues.getArgumentCount());
                    valueHolders.addAll(resolvedValues.getIndexedArgumentValues().values());
                    valueHolders.addAll(resolvedValues.getGenericArgumentValues());
                    for(ValueHolder value: valueHolders){
                        String argType = (value.getType()
                                                  != null?ClassUtils.getShortName(value.getType()):(value.getValue()
                                                                                                            != null?
                                                                                                    value
                                                                                                            .getValue()
                                                                                                            .getClass()
                                                                                                            .getSimpleName():"null"));
                        argTypes.add(argType);
                    }
                }
                String argDesc = StringUtils.collectionToCommaDelimitedString(argTypes);
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "No matching factory method found: "
                                + (mbd.getFactoryBeanName() != null?"factory bean '"
                                + mbd.getFactoryBeanName()
                                + "'; ":"")
                                + "factory method '"
                                + mbd.getFactoryMethodName()
                                + "("
                                + argDesc
                                + ")'. "
                                + "Check that a method with the specified name "
                                + (minNrOfArgs > 0?"and arguments ":"")
                                + "exists and that it is "
                                + (isStatic?"static":"non-static")
                                + ".");
            }else if(void.class == factoryMethodToUse.getReturnType()){
                throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid factory method '" + mbd
                        .getFactoryMethodName() + "': needs to have a non-void return type!");
            }else if(ambiguousFactoryMethods != null){
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Ambiguous factory method matches found in bean '"
                                + beanName
                                + "' "
                                + "(hint: specify index/type/name arguments for simple parameters to avoid type "
                                + "ambiguities): "
                                + ambiguousFactoryMethods);
            }

            if(explicitArgs == null && argsHolderToUse != null){
                mbd.factoryMethodToIntrospect = factoryMethodToUse;
                // 将解析的构造函数加入缓存
                argsHolderToUse.storeCache(mbd, factoryMethodToUse);
            }
        }

        bw.setBeanInstance(instantiate(beanName, mbd, factoryBean, factoryMethodToUse, argsToUse));
        return bw;
    }

    private Object instantiate(String beanName, RootBeanDefinition mbd, @Nullable Object factoryBean,
                               Method factoryMethod, Object[] args) {

        try{
            if(System.getSecurityManager() != null){
                return AccessController.doPrivileged((PrivilegedAction<Object>)() -> this.beanFactory.getInstantiationStrategy()
                                                                                                     .instantiate(mbd
                                                                                                             ,
                                                                                                             beanName
                                                                                                             ,
                                                                                                             this.beanFactory, factoryBean, factoryMethod, args), this.beanFactory
                        .getAccessControlContext());
            }else{
                // 通过执行工厂方法来创建bean示例
                return this.beanFactory.getInstantiationStrategy()
                                       .instantiate(mbd, beanName, this.beanFactory, factoryBean, factoryMethod, args);
            }
        }catch(Throwable ex){
            throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean instantiation via factory "
                    + "method failed", ex);
        }
    }

    /**
     * Resolve the constructor arguments for this bean into the resolvedValues object.
     * This may involve looking up other beans.
     * <p>This method is also used for handling invocations of static factory methods.
     */
    private int resolveConstructorArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
                                            ConstructorArgumentValues cargs, ConstructorArgumentValues resolvedValues) {

        TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
        TypeConverter converter = (customConverter != null?customConverter:bw);
        BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd,
                converter);

        int minNrOfArgs = cargs.getArgumentCount();

        for(Map.Entry<Integer, ConstructorArgumentValues.ValueHolder> entry: cargs.getIndexedArgumentValues()
                                                                                  .entrySet()){
            int index = entry.getKey();
            if(index < 0){
                throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                        "Invalid constructor argument index: "
                                + index);
            }
            if(index > minNrOfArgs){
                minNrOfArgs = index + 1;
            }
            ConstructorArgumentValues.ValueHolder valueHolder = entry.getValue();
            if(valueHolder.isConverted()){
                resolvedValues.addIndexedArgumentValue(index, valueHolder);
            }else{
                Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument",
                        valueHolder.getValue());
                ConstructorArgumentValues.ValueHolder resolvedValueHolder =
                        new ConstructorArgumentValues.ValueHolder(resolvedValue, valueHolder
                        .getType(), valueHolder.getName());
                resolvedValueHolder.setSource(valueHolder);
                resolvedValues.addIndexedArgumentValue(index, resolvedValueHolder);
            }
        }

        for(ConstructorArgumentValues.ValueHolder valueHolder: cargs.getGenericArgumentValues()){
            if(valueHolder.isConverted()){
                resolvedValues.addGenericArgumentValue(valueHolder);
            }else{
                Object resolvedValue = valueResolver.resolveValueIfNecessary("constructor argument",
                        valueHolder.getValue());
                ConstructorArgumentValues.ValueHolder resolvedValueHolder =
                        new ConstructorArgumentValues.ValueHolder(resolvedValue, valueHolder
                        .getType(), valueHolder.getName());
                resolvedValueHolder.setSource(valueHolder);
                resolvedValues.addGenericArgumentValue(resolvedValueHolder);
            }
        }

        return minNrOfArgs;
    }

    /**
     * Create an array of arguments to invoke a constructor or factory method,
     * given the resolved constructor argument values.
     */
    private ArgumentsHolder createArgumentArray(String beanName, RootBeanDefinition mbd,
                                                @Nullable ConstructorArgumentValues resolvedValues, BeanWrapper bw,
                                                Class<?>[] paramTypes, @Nullable String[] paramNames,
                                                Executable executable, boolean autowiring, boolean fallback) throws UnsatisfiedDependencyException {

        TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
        TypeConverter converter = (customConverter != null?customConverter:bw);

        ArgumentsHolder args = new ArgumentsHolder(paramTypes.length);
        Set<ConstructorArgumentValues.ValueHolder> usedValueHolders = new HashSet<>(paramTypes.length);
        Set<String> autowiredBeanNames = new LinkedHashSet<>(4);

        for(int paramIndex = 0; paramIndex < paramTypes.length; paramIndex++){
            Class<?> paramType = paramTypes[paramIndex];
            String paramName = (paramNames != null?paramNames[paramIndex]:"");
            // Try to find matching constructor argument value, either indexed or generic.
            ConstructorArgumentValues.ValueHolder valueHolder = null;
            if(resolvedValues != null){
                valueHolder = resolvedValues.getArgumentValue(paramIndex, paramType, paramName, usedValueHolders);
                // If we couldn't find a direct match and are not supposed to autowire,
                // let's try the next generic, untyped argument value as fallback:
                // it could match after type conversion (for example, String -> int).
                if(valueHolder == null && (!autowiring || paramTypes.length == resolvedValues.getArgumentCount())){
                    valueHolder = resolvedValues.getGenericArgumentValue(null, null, usedValueHolders);
                }
            }
            if(valueHolder != null){
                // We found a potential match - let's give it a try.
                // Do not consider the same value definition multiple times!
                usedValueHolders.add(valueHolder);
                Object originalValue = valueHolder.getValue();
                Object convertedValue;
                if(valueHolder.isConverted()){
                    convertedValue = valueHolder.getConvertedValue();
                    args.preparedArguments[paramIndex] = convertedValue;
                }else{
                    MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                    try{
                        convertedValue = converter.convertIfNecessary(originalValue, paramType, methodParam);
                    }catch(TypeMismatchException ex){
                        throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName,
                                new InjectionPoint(methodParam),
                                "Could not convert argument value of type ["
                                        + ObjectUtils.nullSafeClassName(valueHolder.getValue())
                                        + "] to required type ["
                                        + paramType.getName()
                                        + "]: "
                                        + ex.getMessage());
                    }
                    Object sourceHolder = valueHolder.getSource();
                    if(sourceHolder instanceof ConstructorArgumentValues.ValueHolder){
                        Object sourceValue = ((ConstructorArgumentValues.ValueHolder)sourceHolder).getValue();
                        args.resolveNecessary = true;
                        args.preparedArguments[paramIndex] = sourceValue;
                    }
                }
                args.arguments[paramIndex] = convertedValue;
                args.rawArguments[paramIndex] = originalValue;
            }else{
                MethodParameter methodParam = MethodParameter.forExecutable(executable, paramIndex);
                // No explicit match found: we're either supposed to autowire or
                // have to fail creating an argument array for the given constructor.
                if(!autowiring){
                    throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName,
                            new InjectionPoint(methodParam),
                            "Ambiguous argument values for parameter of type ["
                                    + paramType.getName()
                                    + "] - did you specify the correct bean references as arguments?");
                }
                try{
                    Object autowiredArgument = resolveAutowiredArgument(methodParam, beanName, autowiredBeanNames,
                            converter, fallback);
                    args.rawArguments[paramIndex] = autowiredArgument;
                    args.arguments[paramIndex] = autowiredArgument;
                    args.preparedArguments[paramIndex] = autowiredArgumentMarker;
                    args.resolveNecessary = true;
                }catch(BeansException ex){
                    throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName,
                            new InjectionPoint(methodParam), ex);
                }
            }
        }

        for(String autowiredBeanName: autowiredBeanNames){
            this.beanFactory.registerDependentBean(autowiredBeanName, beanName);
            if(logger.isDebugEnabled()){
                logger.debug("Autowiring by type from bean name '"
                        + beanName
                        + "' via "
                        + (executable instanceof Constructor?"constructor":"factory method")
                        + " to bean named '"
                        + autowiredBeanName
                        + "'");
            }
        }

        return args;
    }

    /**
     * Resolve the prepared arguments stored in the given bean definition.
     */
    private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
                                              Executable executable, Object[] argsToResolve, boolean fallback) {

        TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
        TypeConverter converter = (customConverter != null?customConverter:bw);
        BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd,
                converter);
        Class<?>[] paramTypes = executable.getParameterTypes();

        Object[] resolvedArgs = new Object[argsToResolve.length];
        for(int argIndex = 0; argIndex < argsToResolve.length; argIndex++){
            Object argValue = argsToResolve[argIndex];
            MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
            if(argValue == autowiredArgumentMarker){
                argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
            }else if(argValue instanceof BeanMetadataElement){
                argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
            }else if(argValue instanceof String){
                argValue = this.beanFactory.evaluateBeanDefinitionString((String)argValue, mbd);
            }
            Class<?> paramType = paramTypes[argIndex];
            try{
                resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
            }catch(TypeMismatchException ex){
                throw new UnsatisfiedDependencyException(mbd.getResourceDescription(), beanName,
                        new InjectionPoint(methodParam),
                        "Could not convert argument value of type ["
                                + ObjectUtils.nullSafeClassName(argValue)
                                + "] to required type ["
                                + paramType.getName()
                                + "]: "
                                + ex.getMessage());
            }
        }
        return resolvedArgs;
    }

    protected Constructor<?> getUserDeclaredConstructor(Constructor<?> constructor) {
        Class<?> declaringClass = constructor.getDeclaringClass();
        Class<?> userClass = ClassUtils.getUserClass(declaringClass);
        if(userClass != declaringClass){
            try{
                return userClass.getDeclaredConstructor(constructor.getParameterTypes());
            }catch(NoSuchMethodException ex){
                // No equivalent constructor on user class (superclass)...
                // Let's proceed with the given constructor as we usually would.
            }
        }
        return constructor;
    }

    /**
     * Template method for resolving the specified argument which is supposed to be autowired.
     */
    @Nullable
    protected Object resolveAutowiredArgument(MethodParameter param, String beanName,
                                              @Nullable Set<String> autowiredBeanNames, TypeConverter typeConverter,
                                              boolean fallback) {

        Class<?> paramType = param.getParameterType();
        if(InjectionPoint.class.isAssignableFrom(paramType)){
            InjectionPoint injectionPoint = currentInjectionPoint.get();
            if(injectionPoint == null){
                throw new IllegalStateException("No current InjectionPoint available for " + param);
            }
            return injectionPoint;
        }
        try{
            return this.beanFactory.resolveDependency(new DependencyDescriptor(param, true), beanName,
                    autowiredBeanNames, typeConverter);
        }catch(NoUniqueBeanDefinitionException ex){
            throw ex;
        }catch(NoSuchBeanDefinitionException ex){
            if(fallback){
                // Single constructor or factory method -> let's return an empty array/collection
                // for e.g. a vararg or a non-null List/Set/Map parameter.
                if(paramType.isArray()){
                    return Array.newInstance(paramType.getComponentType(), 0);
                }else if(CollectionFactory.isApproximableCollectionType(paramType)){
                    return CollectionFactory.createCollection(paramType, 0);
                }else if(CollectionFactory.isApproximableMapType(paramType)){
                    return CollectionFactory.createMap(paramType, 0);
                }
            }
            throw ex;
        }
    }

    static InjectionPoint setCurrentInjectionPoint(@Nullable InjectionPoint injectionPoint) {
        InjectionPoint old = currentInjectionPoint.get();
        if(injectionPoint != null){
            currentInjectionPoint.set(injectionPoint);
        }else{
            currentInjectionPoint.remove();
        }
        return old;
    }

    /**
     * Private inner class for holding argument combinations.
     */
    private static class ArgumentsHolder {

        public final Object[] rawArguments;

        public final Object[] arguments;

        public final Object[] preparedArguments;

        public boolean resolveNecessary = false;

        public ArgumentsHolder(int size) {
            this.rawArguments = new Object[size];
            this.arguments = new Object[size];
            this.preparedArguments = new Object[size];
        }

        public ArgumentsHolder(Object[] args) {
            this.rawArguments = args;
            this.arguments = args;
            this.preparedArguments = args;
        }

        public int getTypeDifferenceWeight(Class<?>[] paramTypes) {
            // If valid arguments found, determine type difference weight.
            // Try type difference weight on both the converted arguments and
            // the raw arguments. If the raw weight is better, use it.
            // Decrease raw weight by 1024 to prefer it over equal converted weight.
            int typeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.arguments);
            int rawTypeDiffWeight = MethodInvoker.getTypeDifferenceWeight(paramTypes, this.rawArguments) - 1024;
            return Math.min(rawTypeDiffWeight, typeDiffWeight);
        }

        public int getAssignabilityWeight(Class<?>[] paramTypes) {
            for(int i = 0; i < paramTypes.length; i++){
                if(!ClassUtils.isAssignableValue(paramTypes[i], this.arguments[i])){
                    return Integer.MAX_VALUE;
                }
            }
            for(int i = 0; i < paramTypes.length; i++){
                if(!ClassUtils.isAssignableValue(paramTypes[i], this.rawArguments[i])){
                    return Integer.MAX_VALUE - 512;
                }
            }
            return Integer.MAX_VALUE - 1024;
        }

        public void storeCache(RootBeanDefinition mbd, Executable constructorOrFactoryMethod) {
            synchronized(mbd.constructorArgumentLock){
                mbd.resolvedConstructorOrFactoryMethod = constructorOrFactoryMethod;
                mbd.constructorArgumentsResolved = true;
                if(this.resolveNecessary){
                    mbd.preparedConstructorArguments = this.preparedArguments;
                }else{
                    mbd.resolvedConstructorArguments = this.arguments;
                }
            }
        }

    }

    /**
     * Delegate for checking Java 6's {@link ConstructorProperties} annotation.
     */
    private static class ConstructorPropertiesChecker {

        @Nullable
        public static String[] evaluate(Constructor<?> candidate, int paramCount) {
            ConstructorProperties cp = candidate.getAnnotation(ConstructorProperties.class);
            if(cp != null){
                String[] names = cp.value();
                if(names.length != paramCount){
                    throw new IllegalStateException("Constructor annotated with @ConstructorProperties but not "
                            + "corresponding to actual number of parameters ("
                            + paramCount
                            + "): "
                            + candidate);
                }
                return names;
            }else{
                return null;
            }
        }

    }

}
